package basicfiletransfer;

class ExampleClient
{
	static var host = new flash.text.TextField();
	static var port = new flash.text.TextField();
	static var status = new flash.text.TextField();

	public static function main()
	{
		
		flash.Lib.current.stage.addEventListener(
			flash.events.KeyboardEvent.KEY_DOWN, keypress);
		
		port.y = 35;
		status.y = 70;

		host.type = flash.text.TextFieldType.INPUT;
		port.type = flash.text.TextFieldType.INPUT;

		host.text = "localhost";
		port.text = "10074";
		
		flash.Lib.current.addChild(host);
		flash.Lib.current.addChild(port);
		flash.Lib.current.addChild(status);

		port.width = 800;
		host.width = 800;
		status.width = 800;
		
	}

	public static function update(text : String)
	{
		try
		{
		status.text = FileClient.client.indata.toString();
		}
		catch(d : Dynamic) {status.text=text;}
	}

	public static function keypress(e : flash.events.KeyboardEvent)
	{
		var char = Std.chr(e.charCode);
		var parser = new RequestParser();
		
		switch(char)
		{
			case("s"):
				FileClient.client = 
				new FileClient(host.text,Std.parseInt(port.text),
					update, parser);		
				FileClient.client.uploadrequest(
					RequestParser.r_test,null);
			case("r"):
				FileClient.client = 
				new FileClient(host.text,Std.parseInt(port.text),
					update, parser);		
				FileClient.client.downloadrequest(
					RequestParser.r_test,null);
		}		
	}

}
